package com.bussiness.system.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.List;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.bussiness.system.entity.UserEntity;
import com.bussiness.system.face.UserService;
import com.bussiness.system.jwt.JWT;
import com.bussiness.system.shiro.ShiroUtils;
import com.bussiness.system.util.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.bussiness.system.api.UserControllerApi;

import javax.servlet.http.HttpServletResponse;

/**
 * 系统用户
 *
 * @author xianliru
 * @email 402701653@qq.com
 * @date 2018-08-13 23:23:21
 */
@RestController
@Api(tags = "1.1", description = "PC端::用户管理[缐力如]")
public class UserController implements UserControllerApi {

    @Autowired
    private UserService userService;


    @Autowired
    JWT jwt;

    /**
     * 列表
     */
    @Override
    public Result list(@RequestBody UserEntity query){
        List<UserEntity> page = userService.queryPage(query);

        return new Result<List<UserEntity>>().success(page);
    }

    @Override
    public Result login(@RequestBody UserEntity entity, HttpServletResponse response){
        boolean flag = false;
        Wrapper<UserEntity> wrapper = new EntityWrapper <>(  );
        wrapper.ne( "username",entity.getUsername() );
        List<UserEntity> userEntities = userService.selectList( wrapper );
        for (UserEntity userEntity : userEntities) {
            if(userEntity.getPassword().equals( entity.getPassword() )){
                flag = true;
                break;
            }
        }
        if(!flag){
            return new Result(  ).fail( 0001,"用户或者密码错误！" );
        }
        String token = jwt.generateToken( entity.getUserId() );
        response.setHeader( "Authorization",token );
        return new Result(  ).success( token );
    }

    /**
     * 信息
     */
    @Override
    public Result info(@PathVariable("userId") Long userId){
			UserEntity user = userService.selectById(userId);

        return new Result<UserEntity>().success(user);
    }

    /**
     * 保存
     */
    @Override
    public Result save(@RequestBody UserEntity user){
        user.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setSalt(salt);
        user.setPassword( ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        userService.insert(user);
        return new Result<>().success();
    }

    /**
     * 修改
     */
    @Override
    public Result update(@RequestBody UserEntity user){

		userService.updateById(user);
        return new Result<>().success();
    }

    /**
     * 删除
     */
    @Override
    public Result delete(@RequestBody Long[] userIds){
		userService.deleteBatchIds(Arrays.asList(userIds));
        return new Result<>().success();
    }

}
